<?php
class AewController extends BaseController
{
    // {$url}/aew/sms
    public function smsAction()
    {
        $db = $this->mysql();

        $config    = $db->select('config', '*');
        $webConfig = [];
        foreach ($config as $key => $value) {
            $webConfig[$value['name']] = $value['value'];
        }

        $data = $db->select('aggregation_channel','*',[
            'deletetime'=>null,
        ]);
        foreach ($data as $key => $value) {
           if(substr_count($webConfig['smschannel'], $value['channel_number']) > 0){
           		if($webConfig['warning_threshold']>$value['account_balance']){
           			$obj = new \Sms\Sms();
           			$code = mt_rand(1,666);
           			$result = $obj->sendCode([
           				'smsurl'	=> $webConfig['smsurl'],
           				'smsuser'	=> $webConfig['smsuser'],
           				'smspassword'	=> $webConfig['smspassword'],
           				'mobile'	=> $webConfig['smsmobile'],
           				'content'	=> "【kolay】您的{$value['channel_name']},余额过低，请注意处理"
           			]);
           			echo $result.'<br>';
           		}
           }
        }
        // $db->action(function ($database) {
        //     // 清理已扣除余额，未成功支付，订单
        //     $order = $database->select('cecharge_order', '*', [
        //         'createtime[<]'     => time() - 300,
        //         'paytime'           => 0,
        //         'status'            => 'wait',
        //         'balance[>=]'        => 0,
        //         'payment_amount[>]' => 0,
        //     ]);

        //     if (!empty($order)) {
        //         foreach ($order as $key => $value) {
        //             if($value['balance'] >0){
        //                 // 获取上一级用户信息
        //                 $account_balance = $database->get('user', 'account_balance', [
        //                     'user_number' => $value['user_number'],
        //                 ]);
        //                 // 加入资金变动明细
        //                 $database->insert('user_money_log', [
        //                     'order_number' => $value['order_number'],
        //                     'user_number'  => $value['user_number'],
        //                     'before'       => $account_balance,
        //                     'money'        => $value['balance'],
        //                     'after'        => $account_balance + $value['balance'],
        //                     'state'        => '订单超时退款',
        //                     'type'         => '退款',
        //                     'other'        => $value['user_number'],
        //                     'createtime'   => time(),
        //                 ]);
        //                 if ($database->id() < 1) {
        //                     dump_log('退款： ' . $value['order_number'] . ' 状态： 退款失败', 'cash_bonus_error.txt');
        //                     return false;
        //                 }
    
        //                 // 更新钱包数据
        //                 $res = $database->update('user', [
        //                     'account_balance[+]' => $value['balance'],
        //                     // 'withdrawals'        => $card_member,
    
        //                 ], ['user_number' => $value['user_number']]);
        //                 if ($res->rowCount() == 0) {
        //                     dump_log('退款： ' . $value['order_number'] . ' 状态： 更新用户金额失败', 'cash_bonus_error.txt');
        //                     return false;
        //                 }
        //             }
        //             $res = $database->update('cecharge_order', [
        //                 'status'   => 'error',
        //                 'pay_list' => '订单超时',
        //                 'deletetime'     => time()
        //             ], [
        //                 'order_number' => $value['order_number'],
        //             ]);
        //             if ($res->rowCount() == 0) {
        //                 dump_log('退款： ' . $value['order_number'] . ' 状态： 更新用户金额失败', 'cash_bonus_error.txt');
        //                 return false;
        //             }
        //             var_dump('订单：' . $value['order_number'] . ' 成功为' . $value['user_number'] . '退款' . $value['balance'] . '元');

        //         }
        //         return true;
        //     }

        // });

        return false;
    }


}
